﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace Soat.SoPrism.TemplateWizards.Helpers
{

    public class DelegateCommand : ICommand
    {
        public ExecuteWithoutParameterEventHandler ExecuteWithoutParameterEventHandler { get; private set; }
        public CanExecuteWithoutParameterEventHandler CanExecuteWithoutParameterEventHandler { get; private set; }

        protected DelegateCommand()
        {
        }

        public DelegateCommand(ExecuteWithoutParameterEventHandler executeWithoutParameterEventHandler)
            : this(executeWithoutParameterEventHandler, null)
        {

        }

        public DelegateCommand(ExecuteWithoutParameterEventHandler executeWithoutParameterEventHandler, CanExecuteWithoutParameterEventHandler canExecuteWithoutParameterEventHandler)
        {
            this.ExecuteWithoutParameterEventHandler = executeWithoutParameterEventHandler;
            this.CanExecuteWithoutParameterEventHandler = canExecuteWithoutParameterEventHandler;
        }

        public event EventHandler CanExecuteChanged;
        public virtual bool CanExecute(object parameter)
        {
            if (null == CanExecuteWithoutParameterEventHandler)
            {
                return true;
            }
            return CanExecuteWithoutParameterEventHandler();
        }

        public virtual void Execute(object parameter)
        {
            if (null != ExecuteWithoutParameterEventHandler)
            {
                ExecuteWithoutParameterEventHandler();
            }
        }


        protected void OnCanExecuteChanged()
        {
            if (null != CanExecuteChanged)
            {
                CanExecuteChanged(this, new EventArgs());
            }
        }

        public void FireCanExecuteChanged()
        {
            OnCanExecuteChanged();
        }


        public static DelegateCommand Create(ExecuteEventHandler<object> execute)
        {
            return new DelegateCommand<object>(execute);
        }

        public static DelegateCommand Create(ExecuteEventHandler<object> execute, CanExecuteEventHandler<object> canExecute)
        {
            return new DelegateCommand<object>(execute, canExecute);
        }

        public static DelegateCommand Create(ExecuteWithoutParameterEventHandler execute)
        {
            return new DelegateCommand(execute);

        }

        public static DelegateCommand Create(ExecuteWithoutParameterEventHandler execute, CanExecuteWithoutParameterEventHandler canExecute)
        {
            return new DelegateCommand(execute, canExecute);
        }

        public static DelegateCommand Create<T>(ExecuteEventHandler<T> execute)
        {
            return new DelegateCommand<T>(execute);
        }

        public static DelegateCommand Create<T>(ExecuteEventHandler<T> execute, CanExecuteEventHandler<T> canExecute)
        {
            return new DelegateCommand<T>(execute, canExecute);
        }


        public virtual string ExecuteTargetName
        {
            get
            {
                return ExecuteWithoutParameterEventHandler.Method.Name;
            }
        }

        public virtual string CanExecuteTargetName
        {
            get
            {
                return CanExecuteWithoutParameterEventHandler.Method.Name;
            }
        }

        public virtual Type ExecuteTargetType
        {
            get
            {
                return ExecuteWithoutParameterEventHandler.Target.GetType();
            }
        }

        public virtual Type CanExecuteTargetType
        {
            get
            {
                return CanExecuteWithoutParameterEventHandler.Target.GetType();
            }
        }
    }
}
